#!/bin/bash

echo case.1: while拥有改变变量的能力
Array=()

cnt=1
while [ $cnt -le 5 ]; do
    Array+=($cnt)
    ((cnt++))
done
echo before ${Array[@]}

echo case.2: 在管道里while无法改变管道外的变量
Array=()
echo -e "1\n2\n3\n4\n5" | while read str; do
    Array+=($str)
    echo ${Array[@]}
done
echo after ${Array[@]}

echo case.3: 逐行读取还改变变量的例子
task_output=$(echo -e "1\n2\n3\n4\n5")
Array=()
while read str; do
    Array+=($str)
    echo ${Array[@]}
done <<< "$task_output"
echo final: ${Array[@]}
